<?php
namespace app\car\controller;
use app\AdminRest;

use app\ApiRest;
use app\car\model\ArticleCate;
use app\car\model\ArticleList;
use app\car\model\CarCate;
use app\car\model\CarInfo;
use app\car\model\CarList;
use app\car\model\CarSetting;
use app\car\model\ScreenColor;
use app\car\model\ScreenDisplacement;
use app\car\model\ScreenMileage;
use app\car\model\ScreenModel;
use app\car\model\ScreenPrice;
use app\car\model\ScreenStandard;
use app\car\model\ScreenYear;
use app\card\model\UserInfo;
use app\diy\model\DiyModel;
use app\shop\model\IndexCompany;
use think\App;
use think\facade\Db;


class IndexCar extends ApiRest
{

    protected $model;

    protected $cate_model;

    protected $site_model;



    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new CarList();

        $this->cate_model  = new CarCate();

        $this->site_model  = new CarSetting();


    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:分类列表
     */
    public function cateList(){

        $input = $this->_param;

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['pid','=',0];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->cate_model->where($dis)->order('top desc,id desc')->select()->toArray();

        $arr = [];

        if(!empty($data)){

            foreach ($data as $v){

                $arr[$v['key']][] = $v;

            }

        }

        ksort($arr);

        return $this->success($arr);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-24 14:31
     * @功能说明:热门品牌
     */
    public function hotCateList(){

        $input = $this->_param;

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['pid','=',0];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->cate_model->where($dis)->order('click_num desc,top desc,id desc')->limit(10)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-01 15:23
     * @功能说明:添加品牌点击数量
     */
    public function addClickNum(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->cate_model->where($dis)->update(['click_num'=>Db::Raw("click_num+1")]);

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:分类详情
     */
    public function cateInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->cate_model->dataInfo($dis);

        $where = [

            'pid' => $data['id'],

            'status' => 1
        ];

        $data['children'] = $this->cate_model->where($where)->select()->toArray();

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:58
     * @功能说明:汽车列表
     */
    public function carList(){

        $input = $this->_param;

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }
        //品牌搜索
        if(!empty($input['cate_id'])){

            $cate_id = $this->cate_model->getSonId($input['cate_id']);

            $dis[] = ['cate_id','in',$cate_id];

        }
        //车型
        if(!empty($input['car_model'])){

            $dis[] = ['car_model','=',$input['car_model']];

        }
        //车龄
        if(!empty($input['car_year'])){

            $year_model = new ScreenYear();

            $car_year = $year_model->dataInfo(['id'=>$input['car_year']]);

            $min_year = time()-$car_year['min_year']*86400*365;

            $max_year = time()-$car_year['max_year']*86400*365;

            $dis[] = ['licensing_time','between',"$max_year,$min_year"];

        }
        //价格
        if(!empty($input['price'])){

            $price_model = new ScreenPrice();

            $price = $price_model->dataInfo(['id'=>$input['price']]);

            $dis[] = ['price','between',"{$price['min_price']},{$price['max_price']}"];

        }

        if(isset($input['min_price'])&&!empty($input['max_price'])){

            $dis[] = ['price','between',"{$input['min_price']},{$input['max_price']}"];
        }
        //里程
        if(!empty($input['mileage'])){

            $mileage_model = new ScreenMileage();

            $mileage = $mileage_model->dataInfo(['id'=>$input['mileage']]);

//            dump($mileage);exit;

            if($mileage['type']==1){

                $dis[] = ['mileage','<',$mileage['mileage']];

            }else{

                $dis[] = ['mileage','>',$mileage['mileage']];
            }

        }
        //颜色
        if(!empty($input['color'])){

            $dis[] = ['color','=',$input['color']];

        }
        //排量
        if(!empty($input['displacement'])){

            $displacement_model = new ScreenDisplacement();

            $displacement = $displacement_model->dataInfo(['id'=>$input['displacement']]);

            $dis[] = ['displacement_unit','=',$displacement['unit']];

            $dis[] = ['displacement','between',"{$displacement['min_displacement']},{$displacement['max_displacement']}"];
        }
        //标准排放
        if(!empty($input['emission_standard'])){

            $dis[] = ['emission_standard','=',$input['emission_standard']];

        }

        if(!empty($input['transmission_case'])){

            $dis[] = ['transmission_case','=',$input['transmission_case']];
        }

        $sort = !empty($input['sort'])?$input['sort']:0;

        $data = $this->model->indexDataList($dis,$sort,10);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['licensing_time'] = date('Y',$v['licensing_time']).'年'.date('m',$v['licensing_time']).'月';

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-11 14:59
     * @功能说明:商城首页diy版本
     */
    public function carListDiy(){
        //获取diy的数据
        $diy_data = DiyModel::where([['status', '=', 1],['uniacid', '=', $this->_uniacid]])->find();
        //获取商城的
        $page_data= json_decode($diy_data['page'], true)[51] ?? [];

        //判断有没有diy过
        if(empty($page_data)|| (array_key_exists('list',$page_data) && empty($page_data['list']))){

            $page_data = longbing_default_Page(51);

        }
        //获取参数
        $input    = $this->_param;
        //获取数据
        foreach ($page_data['list'] as &$value){
            //获取商品
            if($value['type']=='carList'&&empty($value['data']['dataList'])){

                $dis[] = ['status','=',1];

                $dis[] = ['uniacid','=',$this->_uniacid];

                if(!empty($input['title'])){

                    $dis[] = ['title','like','%'.$input['title'].'%'];
                }
                //品牌搜索
                if(!empty($input['cate_id'])){

                    $cate_id = $this->cate_model->getSonId($input['cate_id']);

                    $dis[] = ['cate_id','in',$cate_id];

                }
                //车型
                if(!empty($input['car_model'])){

                    $dis[] = ['car_model','=',$input['car_model']];

                }
                //车龄
                if(!empty($input['car_year'])){

                    $year_model = new ScreenYear();

                    $car_year = $year_model->dataInfo(['id'=>$input['car_year']]);

                    $min_year = time()-$car_year['min_year']*86400*365;

                    $max_year = time()-$car_year['max_year']*86400*365;

                    $dis[] = ['licensing_time','between',"$max_year,$min_year"];

                }
                //价格
                if(!empty($input['price'])){

                    $price_model = new ScreenPrice();

                    $price = $price_model->dataInfo(['id'=>$input['price']]);

                    $dis[] = ['price','between',"{$price['min_price']},{$price['max_price']}"];

                }

                if(isset($input['min_price'])&&!empty($input['max_price'])){

                    $dis[] = ['price','between',"{$input['min_price']},{$input['max_price']}"];
                }
                //里程
                if(!empty($input['mileage'])){

                    $mileage_model = new ScreenMileage();

                    $mileage = $mileage_model->dataInfo(['id'=>$input['mileage']]);

//            dump($mileage);exit;

                    if($mileage['type']==1){

                        $dis[] = ['mileage','<',$mileage['mileage']];

                    }else{

                        $dis[] = ['mileage','>=',$mileage['mileage']];
                    }

                }
                //颜色
                if(!empty($input['color'])){

                    $dis[] = ['color','=',$input['color']];

                }
                //排量
                if(!empty($input['displacement'])){

                    $displacement_model = new ScreenDisplacement();

                    $displacement = $displacement_model->dataInfo(['id'=>$input['displacement']]);

                    $dis[] = ['displacement_unit','=',$displacement['unit']];

                    $dis[] = ['displacement','between',"{$displacement['min_displacement']},{$displacement['max_displacement']}"];

                }
                //标准排放
                if(!empty($input['emission_standard'])){

                    $dis[] = ['emission_standard','=',$input['emission_standard']];

                }

                if(!empty($input['transmission_case'])){

                    $dis[] = ['transmission_case','=',$input['transmission_case']];
                }

                $sort = !empty($input['sort'])?$input['sort']:0;

                $data = $this->model->indexDataList($dis,$sort,10);

                if(!empty($data['data'])){

                    foreach ($data['data'] as &$v){

                        $v['licensing_time'] = date('Y',$v['licensing_time']).'年'.date('m',$v['licensing_time']).'月';

                    }

                }

                $value['data']['dataList']  = $data;

            }

        }

        return $this->success($page_data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:汽车详情
     */
    public function carInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $data['licensing_time'] = date('Y',$data['licensing_time']).'年'.date('m',$data['licensing_time']).'月';

        $info_model = new CarInfo();

        $dis = [

            'car_id' => $input['id']
        ];

        $data['info'] = $info_model->where($dis)->select()->toArray();

        $color_model   = new ScreenColor();

        $model_model   = new ScreenModel();

        $stand_model   = new ScreenStandard();

        $data['color'] = $color_model->where(['id'=>$data['color']])->value('color');

        $data['emission_standard'] = $stand_model->where(['id'=>$data['emission_standard']])->value('title');

        $data['car_model']         = $model_model->where(['id'=>$data['car_model']])->value('title');

        $data['transmission_case'] = $data['transmission_case']==1?'自动':'手动';

        $data['setting'] = $this->site_model->where('id','in',$data['setting'])->where(['status'=>1])->field('id,cover,title')->select()->toArray();

//        $data['setting'] = array_values($data['setting']);

        $data['staff_info']['user_name']   = lbUserName($input['staff_id']);

        $data['staff_info']['user_avatar'] = lbUserAvatar($input['staff_id']);

        $data['staff_info']['staff_id']    = $input['staff_id'];

        $user_model = new UserInfo();

        $data['staff_info']['phone'] = $user_model->where(['fans_id'=>$input['staff_id']])->value('phone');

        $res = $this->model->where(['id'=>$input['id']])->update(['view_count'=>Db::raw('view_count+1')]);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-03 12:08
     * @功能说明:配置详情
     */
    public function siteInfo(){

        $input = $this->_param;

        $data = $this->site_model->dataInfo(['id'=>$input['id']]);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-26 11:28
     * @功能说明:筛选下拉框
     */
    public function screenSelect(){

        $input = $this->_param;

        $color_model   = new ScreenColor();

        $dis_model     = new ScreenDisplacement();

        $mileage_model = new ScreenMileage();

        $model_model   = new ScreenModel();

        $price_model   = new ScreenPrice();

        $stand_model   = new ScreenStandard();

        $year_model    = new ScreenYear();

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];
        //颜色下拉框
        $data['color_list']        = $color_model->where($dis)->select()->toArray();
        //排量下拉框
        $data['displacement_list'] = $dis_model->where($dis)->select()->toArray();
        //里程下拉框
        $data['mileage_list']      = $mileage_model->where($dis)->select()->toArray();
        //车型下拉框
        $data['model_list']        = $model_model->where($dis)->select()->toArray();
        //价格下拉框
        $data['price_list']        = $price_model->where($dis)->select()->toArray();
        //标准下拉框
        $data['stand_list']        = $stand_model->where($dis)->select()->toArray();
        //车龄下拉框
        $data['year_list']         = $year_model->where($dis)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:颜色筛选列表
     */
    public function colorSelect(){

        $input = $this->_param;

        $color_model = new ScreenColor();

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $color_model->where($dis)->select()->toArray();

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:排量筛选列表
     */
    public function displacementSelect(){

        $input = $this->_param;

        $dis_model = new ScreenDisplacement();

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $data = $dis_model->where($dis)->select()->toArray();

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:里程筛选列表
     */
    public function mileageSelect(){

        $input = $this->_param;

        $mileage_model = new ScreenMileage();

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $mileage_model->where($dis)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:车型筛选列表
     */
    public function modelSelect(){

        $input = $this->_param;

        $model_model = new ScreenModel();

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $model_model->where($dis)->select()->toArray();

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:价格筛选列表
     */
    public function priceSelect(){

        $input = $this->_param;

        $price_model = new ScreenPrice();

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $price_model->where($dis)->select()->toArray();

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:标准筛选列表
     */
    public function standardSelect(){

        $input = $this->_param;

        $stand_model = new ScreenStandard();

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $stand_model->where($dis)->select()->toArray();

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 17:51
     * @功能说明:车龄筛选列表
     */
    public function yearSelect(){

        $input = $this->_param;

        $year_model = new ScreenYear();

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $year_model->where($dis)->select()->toArray();

        return $this->success($data);

    }




















}
